package com.dongrj.framework.core.utils;

import com.dongrj.framework.core.constant.Constant;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Objects;

/**
 * @author dongrongjun
 * @date 2020/4/2
 */
@Slf4j
public class AppUtils {

    private static volatile String APP_ID = "";

    private static volatile String LOCAL_IP = "";

    private static volatile String LOCAL_HOST = "";

    // 机房参数
    private static volatile String SERVER_ROOM = "";

    public final static String DEFAULT_IP = "127.0.0.1";


    /**
     * 获取AppId
     */
    public static String getAppId() {
        if (StringUtils.isNotEmpty(APP_ID)) {
            return APP_ID;
        }
        List<String> lines = getList();
        for (String line : lines) {
            if (line.startsWith("app.id=")) {
                String[] split = line.split("=");
                APP_ID = split[1].trim();
                if (StringUtils.isEmpty(APP_ID)) {
                    throw new RuntimeException("app.id" + "必须被设置!");
                }
                return split[1];
            }
        }
        String property = System.getProperty(Constant.App.APP_ID);
        if (StringUtils.isEmpty(property)) {
            throw new RuntimeException(Constant.App.APP_ID + "必须被设置!");
        }
        APP_ID = property.trim();
        return property;
    }

    /**
     * 获取IP
     */
    public static String getLocalIP() {
        if (StringUtils.isNotEmpty(LOCAL_IP)) {
            return LOCAL_IP;
        }
        InetAddress addr = null;
        try {
            addr = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        if (Objects.isNull(addr)) {
            LOCAL_IP = DEFAULT_IP;
            return DEFAULT_IP;
        }
        byte[] ipAddr = addr.getAddress();
        StringBuilder ipAddrStr = new StringBuilder();
        for (int i = 0; i < ipAddr.length; i++) {
            if (i > 0) {
                ipAddrStr.append(".");
            }
            ipAddrStr.append(ipAddr[i] & 0xFF);
        }
        LOCAL_IP = ipAddrStr.toString();
        if (StringUtils.isEmpty(LOCAL_IP)) {
            LOCAL_IP = DEFAULT_IP;
            return DEFAULT_IP;
        }
        return ipAddrStr.toString();
    }

    private static List<String> getList() {
        List<String> stringList = Lists.newArrayList();
        try {
            InputStream resourceAsStream = AppUtils.class.getResourceAsStream(Constant.App.APP_INFO_FILE);
            BufferedReader r = new BufferedReader(new InputStreamReader(resourceAsStream));
            String line;
            while ((line = r.readLine()) != null) {
                stringList.add(line);
            }
            resourceAsStream.close();
            r.close();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("getAppId异常", e);
        }
        return stringList;
    }


    /**
     * 获取主机名
     *
     * @return 本地主机名
     */
    public static String getHostName() {
        if (StringUtils.isNotEmpty(LOCAL_HOST)) {
            return LOCAL_HOST;
        }
        try {
            String hostName = InetAddress.getLocalHost().getHostName();
            LOCAL_HOST = hostName;
            return hostName;
        } catch (UnknownHostException e) {
            LOCAL_HOST = "AdminName";
            throw new RuntimeException("获取本地主机名异常!");
        }
    }


    /**
     * 判断当前操作系统是不是window
     */
    public static boolean isWindow() {
        String system = System.getProperty("os.name");
        return system.toLowerCase().startsWith("win");
    }


    /**
     * 获取当前Application值
     */
    public static String getApplication() {
        if (StringUtils.isNotEmpty(SERVER_ROOM)) {
            return SERVER_ROOM;
        }
        String elkValue = System.getProperty("server.room");
        SERVER_ROOM = StringUtils.defaultIfEmpty(elkValue, "none");
        return SERVER_ROOM;
    }


}
